package com.king.armips.android.utils;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;

public class ScreenUtils {

    public static int screenWidth;
    public static int screenHeight;

    public static void init(Activity activity){
        DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
        screenWidth = displayMetrics.widthPixels;
        screenHeight = displayMetrics.heightPixels;

        //通过decorView获取屏幕宽高，这样更准确
        View activityRootView = activity.getWindow().getDecorView();
        activityRootView.post(() -> {
            screenWidth = activityRootView.getMeasuredWidth();
            screenHeight = activityRootView.getMeasuredHeight();
        });
    }

    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
